package com.crawler.parser;

import com.crawler.api.CrawlerParser;
import com.crawler.api.CrawlerPipeline;
import com.crawler.model.CrawlerPageInfo;
import com.crawler.model.CrawlerPersistent;
import com.crawler.model.CrawlerRequestInfo;
import com.crawler.pipeline.ConsolePipeline;
import com.crawler.scheduler.HashSetCrawlerscheduler;
import com.crawler.selector.CssUrlSeletor;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;


@Service
@Slf4j
public class DefaultCrawlerParser implements CrawlerParser {

    @Autowired
    HashSetCrawlerscheduler hashSetCrawlerscheduler;

    @Autowired
    ConsolePipeline consolePipeline;

    @Autowired
    CssUrlSeletor cssUrlSeletor;

    @Override
    public void parse(CrawlerPageInfo crawlerPageInfo) {
        String body = crawlerPageInfo.getBody();
        if (null == body) {
            return;
        }
        Document html = Jsoup.parse(body);
        Collection<String> strings = cssUrlSeletor.addUrls(html);
        strings.stream().forEach(e -> {
            CrawlerRequestInfo crawlerRequestInfo = new CrawlerRequestInfo();
            crawlerRequestInfo.setUrl(e);
            crawlerRequestInfo.setMethod("GET");
            log.info("add url:{}",e);
            addRouter(crawlerRequestInfo);
        });
        Elements pageVoElements = html.select("#cnblogs_post_body");
        if (pageVoElements != null && pageVoElements.hasText()) {
            for (Element pageVoElement : pageVoElements) {
                String html1 = pageVoElements.html();
                CrawlerPersistent crawlerPersistent = new CrawlerPersistent();
                crawlerPersistent.setContent(html1);
                consolePipeline.pipeline(crawlerPersistent);

            }}

    }

    /**
     * // href、abs:href
     *
     * @param crawlerRequestInfo
     */
    @Override
    public void addRouter(CrawlerRequestInfo crawlerRequestInfo) {
        hashSetCrawlerscheduler.push(crawlerRequestInfo);
    }


}
